package com.scalar.chiptrack.operations.dataentry.lotcorrection.actionCommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.dataentry.lotcorrection.forms.LotCorrectionForm;
import com.scalar.chiptrack.operations.dataentry.lotcorrection.LotCorrectionHelper;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.actioncommands.ActionCommand;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Ganesh
 * Date: Nov 28, 2006
 * Time: 12:26:16 PM
 */
public class LotCorrectionLoadPartNumbersCommand  extends ActionCommand
{
    private static final String ACTION_NAME = "/lotCorrection.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        LotCorrectionForm lotCorrectionForm = ( LotCorrectionForm ) form;
        LotCorrectionHelper lotCorrectionHelper = LotCorrectionHelper.getInstance();
        String action = request.getParameter("action");
        String processType = lotCorrectionForm.getProcessType();

        HashMap errors = (HashMap)request.getAttribute(ERROR_MESSAGES);
        if ( errors == null )
        {
            errors = new HashMap();
            request.setAttribute(ERROR_MESSAGES, errors );
        }

        HashMap info = (HashMap)request.getAttribute(INFO_MESSAGES);
        if ( info == null )
        {
            info = new HashMap();
            request.setAttribute(INFO_MESSAGES, info );
        }

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        ArrayList partNumbersLableValueBeans;
        try
        {
            lotCorrectionForm.setProcessStepId("0");
            lotCorrectionForm.setLocationId("0");
            lotCorrectionForm.setLotId("0");

            //partNumbersLableValueBeans=lotCorrectionHelper.getProductList(processType);
            partNumbersLableValueBeans = NewProductDAO.loadPartNumbers(null, 0, processType, true , true, true, 0);

            ArrayList processList = lotCorrectionHelper.getProcessList();
            request.setAttribute ("processList", processList);
            request.setAttribute ( "partNumbersList", partNumbersLableValueBeans );
            request.setAttribute ( "action", action );
        }
        catch( Exception e)
        {
           e.printStackTrace();
        }
        return mapping.findForward ( "lotCorrection" );
    }

}
